﻿jQuery.fn.infiniteCarousel_1 = function () {
    function repeat(str, num) {
        return new Array(num + 1).join(str);
    }
    return this.each(function () {
        var jQuerywrapper_2 = jQuery('> div', this).css('overflow', 'hidden'),
            jQueryslider = jQuerywrapper_2.find('> ul'),
            jQueryitems = jQueryslider.find('> li'),
            jQuerysingle = jQueryitems.filter(':first'),
            singleWidth = jQuerysingle.outerWidth(),
            visible = Math.ceil(jQuerywrapper_2.innerWidth() / singleWidth),
            currentPage = 1,
            pages = Math.ceil(jQueryitems.length / visible);
        if ((jQueryitems.length % visible) != 0) {
            jQueryslider.append(repeat('<li class="empty" />', visible - (jQueryitems.length % visible)));
            jQueryitems = jQueryslider.find('> li');
        }
        jQueryitems.filter(':first').before(jQueryitems.slice(-visible).clone().addClass('cloned'));
        jQueryitems.filter(':last').after(jQueryitems.slice(0, visible).clone().addClass('cloned'));
        jQueryitems = jQueryslider.find('> li');
        jQuerywrapper_2.scrollLeft(singleWidth * visible);
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            jQuerywrapper_2.filter(':not(:animated)').animate({
                scrollLeft: '+=' + left
            }, 500, function () {
                if (page == 0) {
                    jQuerywrapper_2.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    jQuerywrapper_2.scrollLeft(singleWidth * visible);
                    page = 1;
                }
                currentPage = page;
            });
            return false;
        }
        jQuerywrapper_2.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        jQuery('a.back', this).click(function () {
            return gotoPage(currentPage - 1);
        });
        jQuery('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        jQuery(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });
};